#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_exam1.py
# author:王雯雯
# datetime:2024-12-7
# software: PyCharm


import requests
from bs4 import BeautifulSoup

url = "https://search.51job.com/list/010000,000000,0000,00,9,99,python,2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare="
response = requests.get(url)
html_content = response.text

soup = BeautifulSoup(html_content, 'html.parser')

salary_tags = soup.select('.el.t4')
salaries = []
for tag in salary_tags:
    salary_text = tag.text.strip()
    if '-' in salary_text:
        low, high = salary_text.split('-')
        low = int(low[:-1]) * 1000 if 'k' in low else int(low)
        high = int(high[:-1]) * 1000 if 'k' in high else int(high)
        salaries.append((low + high) / 2)
    elif 'k' in salary_text:
        salary = int(salary_text[:-1]) * 1000
        salaries.append(salary)
    elif '万' in salary_text:
        salary = int(float(salary_text[:-1]) * 10000)
        salaries.append(salary)
    else:
        print(f"无法解析的薪酬格式: {salary_text}")

if salaries:
    average_salary = sum(salaries) / len(salaries)
    print(f"北京地区Python相关技术工程师的平均薪酬约为: {average_salary}元")
else:
    print("未获取到有效的薪酬信息。")